--
-- Author: shunguo.chen
-- Date: 2018-02-06 11:02:29
--
-- MapView 城市地图界面

local ZoneIconRender = import(".renders.ZoneIconRender")
local MapNpcPanel = import(".MapNpcPanel")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.MapScene

local BLESS_NUM_STR = L("lua_code_text_229")
local YEAR_STR = L("lua_code_text_533")

function ClassRef:init()
	-- top
	self._ui.Button_close:addClickEventListener(function()
			-- if self._isFromWorld then
			-- 	self:hide()
			-- else
			-- 	executeFunc(self._onCloseView)
			-- end
			executeFunc(self._onCloseView)
		end)

	self._ui["Panel/Panel_world/btn_world"]:addClickEventListener(function()
			executeFunc(self._onOperate, "openWorld")
		end)

	self._tltText = self._ui["Panel/Panel_tlt/name"]
	self._yearText = self._ui["Panel/Panel_tlt/time"]

	-- bom
	local bom = self._ui["Panel/Panel_bom"]
	self._descText = bom:getChildByName("text")
	self._numText = bom:getChildByName("num")

	self._renders = {0,0,0}
	for i=1,10 do
		local node = bom:getChildByName("map_" .. i)
		if node then
			local render = ZoneIconRender.create(node)
			render:onBtnClick(handler(self, self.setCurRender))
			self._renders[i] = render
		else
			break
		end
	end

	bom:getChildByName("btn_npc"):addClickEventListener(function()
		self:_openNpcList()
	end)
	bom:getChildByName("btn_go"):addClickEventListener(function()
		local zone = self._curRender:getData()
		local param = {mapId=self._mapInfo.id, pos=zone:getResetPos()}
		executeFunc(self._onOperate, "gotoZone", param)
		executeFunc(self._onCloseView)
	end)

	self._npcPanel = MapNpcPanel.create(self._ui["Panel/npc"])
	self._npcPanel:onBtnClick(function(npcId)
		local param = {mapId=self._mapInfo.id, npcId=npcId}
		executeFunc(self._onOperate, "gotoNpc", param)
		executeFunc(self._onCloseView)
	end)
end

function ClassRef:show()
	self.super.show(self)
end

function ClassRef:_openNpcList()
	local zone = self._curRender:getData()
	local npcs = zone:getNpcs()
	if #npcs > 0 then
		self._npcPanel:refreshList(zone:getNpcs())
	else
		display.pushToast(L("lua_code_text_245"))
	end
end

function ClassRef:_updateBg(path)
	if self._prePath ~= path then
		self._prePath = path
		self._ui.map:loadTexture(path)
	end
end

function ClassRef:setCurRender(render)
	if self._curRender ~= render then
		if self._curRender then
			self._curRender:setSelected(false)
		end
		self._curRender = render
	end

	local zone = render:getData()
	self._descText:setString(zone:getDesc())
	self._numText:setString(string.format(BLESS_NUM_STR, zone:getBlessExp()))
	self:_updateBg(zone:getBg())
end

-- info = MapInfo
-- isFromWorld  是否从世界地图打开
function ClassRef:refresh(mapInfo, isFromWorld)
	self._mapInfo = mapInfo
	self._isFromWorld = isFromWorld

	self._ui["Panel/Panel_world"]:setVisible(not isFromWorld)
	self._tltText:setString(mapInfo:getName())

	local gameDate = app.session:getVirtualTime()
	self._yearText:setString(string.format(YEAR_STR, gameDate.year))

	local isCurMap = self._mapInfo:isCurMap()
	local zones = mapInfo:getZones()
	local findRender
	for i,render in ipairs(self._renders) do
		local data = zones[i]
		render:setVisible(data ~= nil)
		if data then
			render:setData(data)
			render:setIsCurMap(isCurMap)
			if isCurMap and data:isCurZone() then
				findRender = render
			end
		end
	end

	findRender = findRender or self._renders[1]
	findRender:setSelected(true)
	self:setCurRender(findRender)
end

return ClassRef